import { createRouter, createWebHistory } from 'vue-router';
import type { RouteRecordRaw } from 'vue-router';
import MainLayout from '../components/MainLayout.vue';

// 定义路由规则
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'MainLayout',
    component: MainLayout,
    redirect: '/dashboard',
    children: [
      // 仪表盘
      {
        path: 'dashboard',
        name: 'Dashboard',
        meta: { title: '仪表盘', icon: 'HomeIcon' },
        component: () => import('../views/Dashboard.vue')
      },

      // 供应商管理
      {
        path: 'supplier',
        name: 'Supplier',
        meta: { title: '供应商管理', icon: 'UserFilledIcon' },
        redirect: '/supplier/list',
        children: [
          {
            path: 'list',
            name: 'SupplierList',
            meta: { title: '供应商列表' },
            component: () => import('../views/supplier/List.vue')
          },
          {
            path: 'audit',
            name: 'SupplierAudit',
            meta: { title: '资质审核' },
            component: () => import('../views/supplier/Audit.vue')
          },
          {
            path: 'risk',
            name: 'SupplierRisk',
            meta: { title: '风险评级' },
            component: () => import('../views/supplier/Risk.vue')
          },
          {
            path: 'detail/:id',
            name: 'SupplierDetail',
            meta: { title: '供应商详情' },
            component: () => import('../views/supplier/Detail.vue'),
            hidden: true
          }
        ]
      },

      // 订单管理
      {
        path: 'order',
        name: 'Order',
        meta: { title: '订单管理', icon: 'ShoppingCartIcon' },
        redirect: '/order/list',
        children: [
          {
            path: 'list',
            name: 'OrderList',
            meta: { title: '订单列表' },
            component: () => import('../views/order/List.vue')
          },
          {
            path: 'sync',
            name: 'OrderSync',
            meta: { title: '订单同步' },
            component: () => import('../views/order/Sync.vue')
          },
          {
            path: 'abnormal',
            name: 'OrderAbnormal',
            meta: { title: '异常处理' },
            component: () => import('../views/order/Abnormal.vue')
          },
          {
            path: 'detail/:id',
            name: 'OrderDetail',
            meta: { title: '订单详情' },
            component: () => import('../views/order/Detail.vue'),
            hidden: true
          }
        ]
      },

      // 财务管理
      {
        path: 'finance',
        name: 'Finance',
        meta: { title: '财务管理', icon: 'WalletIcon' },
        redirect: '/finance/invoice',
        children: [
          {
            path: 'invoice',
            name: 'Invoice',
            meta: { title: '发票管理' },
            component: () => import('../views/finance/Invoice.vue')
          },
          {
            path: 'reconciliation',
            name: 'Reconciliation',
            meta: { title: '对账管理' },
            component: () => import('../views/finance/Reconciliation.vue')
          },
          {
            path: 'financial-report',
            name: 'FinancialReport',
            meta: { title: '财务报表' },
            component: () => import('../views/finance/Report.vue')
          }
        ]
      },

      // 数据分析
      {
        path: 'analysis',
        name: 'Analysis',
        meta: { title: '数据分析', icon: 'PieChartIcon' },
        redirect: '/analysis/supplier',
        children: [
          {
            path: 'supplier',
            name: 'SupplierAnalysis',
            meta: { title: '供应商分析' },
            component: () => import('../views/analysis/Supplier.vue')
          },
          {
            path: 'order',
            name: 'OrderAnalysis',
            meta: { title: '订单分析' },
            component: () => import('../views/analysis/Order.vue')
          },
          {
            path: 'performance',
            name: 'PerformanceAnalysis',
            meta: { title: '履约分析' },
            component: () => import('../views/analysis/Performance.vue')
          }
        ]
      },

      // 系统设置
      {
        path: 'system',
        name: 'System',
        meta: { title: '系统设置', icon: 'SettingIcon' },
        component: () => import('../views/system/Index.vue')
      }
    ]
  },

  // 登录页
  {
    path: '/login',
    name: 'Login',
    meta: { title: '登录' },
    component: () => import('../views/Login.vue')
  },

  // 404页面
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    meta: { title: '页面不存在' },
    component: () => import('../views/NotFound.vue')
  }
];

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    // 保持滚动位置
    return savedPosition || { top: 0 };
  }
});

// 路由守卫 - 设置页面标题
router.beforeEach((to, from, next) => {
  document.title = to.meta.title ? `${to.meta.title} - 政采云CRM系统` : '政采云CRM系统';
  next();
});

export default router;